/**
 * 
 */
package utils;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author Administrator
 *
 */
public class StringUtil {

	
	/** 
     * 把中文转成Unicode码 
     * @param str 
     * @return 
     */  
    public static String chinaToUnicode(String str){  
        String result="";  
        for (int i = 0; i < str.length(); i++){  
            int chr1 = (char) str.charAt(i);  
            if(chr1>=19968&&chr1<=171941){//汉字范围 \u4e00-\u9fa5 (中文)  
                result+="\\u" + Integer.toHexString(chr1);  
            }else{  
                result+=str.charAt(i);  
            }  
        }  
        return result;  
    }  
	
	
	/**
	 * 替换
	 * @param src
	 * @param replacestr
	 * @param destination
	 * @return
	 */
	public static String strReplaceAll(String src,String replacestr,String destination) {
		if(null == src || "".equals(src)) return "";
		return src.replaceAll(replacestr, destination);
	}
	
	
	/**
	 * 替换多 图片的domain
	 * @param src
	 * @param replacestr
	 * @param destination
	 * @return
	 */
	public static String picMoreUrlConvert(String src) {
		if(null == src || "".equals(src)) return "";
		String regexpress = "\\{\\S+\\-s\\S+},";
		return src.replaceAll(regexpress, "");
	}
	
	
	
	/**
	 * 获取系统版本
	 * @param version
	 * @return
	 */
	public static String getSysVersion(String version){
		if(null == version || "".equals(version)) return "";
		int index = version.indexOf("_");
		if(index > 0){
			return version.substring(0,index);
		}
		return version;
	}
	
	
	/**
	 * 字符串处理，去掉html
	 * @param src
	 * @return
	 */
	public static String htmlRemove(String src) {
		if(null == src || "".equals(src)) return "";
		String regexpress = "<[^>^(br)]+>";
		String strchar = "\r\n\t";
		String strchar2 = "</p>";
		String strchar3 = "<span[^>]*>([/s/S]*?)";
		String strchar4 = "&nbsp;";
		String strchar5 = "</strong>";
		String strchar6 = "\r\n";
		String strchar7 = "</b>";
		String strchar8 = "<p[^>]*>([/s/S]*?)";
		String strchar9 = " ";
		String strchar10 = "\n";
		String strchar11 = "\t";
		
		String str = "";
		//str = str.replaceAll("<span>", newChar)
		str = src.replaceAll(regexpress, "");
		str = str.replaceAll(strchar, "");
		str = str.replaceAll(strchar2, "");
		str = str.replaceAll(strchar3, "");
		str = str.replaceAll(strchar4, "");
		str = str.replaceAll(strchar5, "");
		str = str.replaceAll(strchar6, "");
		str = str.replaceAll(strchar7, "");
		str = str.replaceAll(strchar8, "");
		str = str.replaceAll(strchar9, "");
		str = str.replaceAll(strchar10, "");
		str = str.replaceAll(strchar11, "");
		return str.trim();
	}
	
	public static String brRemove(String src) {
		if(null == src || "".equals(src)) return "";
		String regexpress = "<[^>]+>";
		
		String str = "";
		str = src.replaceAll(regexpress, "");
		return str.trim();
	}
	
	/**
	 * 字符串处理，去掉html
	 * @param src
	 * @return
	 */
	public static String removeImg(String src) {
		if(null == src || "".equals(src)) return "";
		String strchar = "<img[^>]*>([/s/S]*?)";
		String str = src.replaceAll(strchar, "");
		return str.trim();
	}
	
	/**
	 * 全角转换半角
	 * 
	 * @param input
	 * @return
	 */
	public static String toDBC(String input) {
		char[] c = input.toCharArray();
		for (int i = 0; i < c.length; i++) {
			if (c[i] == 12288) {
				c[i] = (char) 32;
				continue;
			}
			if (c[i] > 65280 && c[i] < 65375)
				c[i] = (char) (c[i] - 65248);
		}
		return new String(c);
	}

	/**
	 * 去除特殊字符或将所有中文标号替换为英文标号
	 * 
	 * @param str
	 * @return
	 */
	public static String stringFilter(String str) {
		str = str.replaceAll("【", "[").replaceAll("】", "]")
				.replaceAll("！", "!").replaceAll("：", ":");// 替换中文标号
		String regEx = "[『』]"; // 清除掉特殊字符
		Pattern p = Pattern.compile(regEx);
		Matcher m = p.matcher(str);
		return m.replaceAll("").trim();
	}
	
	/**
	 * 判断对象是否是null或者空字符串
	 * @param str
	 * @return 是true，否false
	 */
	public static boolean isBlank(Object str) {
		if(str==null||str.toString().trim().length()==0) {
			return true;
		} else {
			return false;
		}
	}

	public static String obj2str(Object obj){
    	return null == obj ? "" : String.valueOf(obj);
    }
    
    public static int obj2int(Object obj){
    	return null == obj ? -1 : Integer.parseInt(String.valueOf(obj));
    }
	

	/**
	 * @param args
	 * @throws UnsupportedEncodingException 
	 */
	public static void main(String[] args) throws UnsupportedEncodingException {
//		String str = "[{\"pic\":\"/upload/demo/440-440/%E9%92%9F%E6%B0%B4%E9%A5%BA.jpg\"},{\"pic\":\"/upload/demo/440-440/%E9%92%9F%E6%B0%B4%E9%A5%BA.jpg\"}]";
//		String regexpress = "[^:]+:\"([^,]+)\"}.*";
//		String ret = picurlConvert(str);
//		// System.out.println(ret);
		
//		
//		String str = "[{\"pic\":\"/upload/3/product/20120428/192-s.jpg\"},{\"pic\":\"/upload/3/product/20120428/192-b.jpg\"}]";
//		String regexpress = "\\{\\S+\\-s\\S+},";
//		
////		Pattern p = Pattern. compile(regexpress);
////		Matcher m = p. matcher(str);
////		boolean b = m. matches();
////		int matchcount = m.groupCount();
////		
////		for(int i=0;i<=matchcount;i++){
////			// System.out.println(" match : " + m.group(i));
////		}
////		// System.out.println(b + " matchcount : " + matchcount);
//		
//		// System.out.println(" matchcount : " + picConvert(str));
//		
////		String ttt = "中国123，历，。磷肥，23８９企业家７８９yuｑｗｅｄｊ奋斗,.89，，";
////		String s = ToDBC(ttt);
////		// System.out.println(" s : " + s);
////		s = stringFilter(ttt);
////		// System.out.println(" s2 : " + s);
//		
////		
//		
////		String ccc = "<p style=\"text-align:left;\">\r\n\t带子</br>，<a href>原价<span >22元</span>/个，现价10元/个";
////		
////		String s = htmlRemove(ccc);
////		// System.out.println(" s2 : " + s);
//		
//		String ss = "/app/20120405/Qu nar_v6_2_2012040597.apk";
//		
//		// System.out.println(URLEncoder.encode(ss,"utf-8"));
		
		//sendSms("您申请注册会员,账户13683251169,密码123456,谢谢!如非本人操作,请忽略此信息。移商-[APP]", "18600405807");
		
	}
	
	public static String isNull(String param){
		if(null==param||"".equals(param)||"null".equals(param)){
			return "";
		}
		return param;
	}
	
	public static String defaultBirthday(String param){
		if(null==param||"".equals(param)){
			return "1985-2-20";
		}
		return param;
	
	}
	
	public static String replaceBlank(String str) {
        String dest = "";
        if (str!=null) {
            Pattern p = Pattern.compile("\t|\r|\n");
            Matcher m = p.matcher(str);
            dest = m.replaceAll("");
        }
        return dest;
	}
	
	
    
    /**
     * 获取网页中图片的地址
     * @param htmlStr
     * @return
     */
	public static List<String> getImgSrc(String htmlStr) {
		String img = "";
		Pattern p_image;
		Matcher m_image;
		List<String> pics = new ArrayList<String>();

		String regEx_img = "<img.*src\\s*=\\s*(.*?)[^>]*?>";
		p_image = Pattern.compile(regEx_img, Pattern.CASE_INSENSITIVE);
		m_image = p_image.matcher(htmlStr);
		while (m_image.find()) {
			img = img + "," + m_image.group();

			Matcher m = Pattern.compile("src\\s*=\\s*\"?(.*?)(\"|>|\\s+)")
					.matcher(img);

			while (m.find()) {
				pics.add(m.group(1));
			}
		}
		return pics;
	}
	/**
	 * string数组转int数组
	 * @param array
	 * @return
	 */
	public static int[] strToInt(String[] array){
		if(array ==null || array.length == 0){
			return null;
		}
		int[] result = new int[array.length];
		for (int i = 0; i < array.length; i++) {
			result[i] = Integer.parseInt(array[i]);
		}
		return result;
	}
	
}
